package com.sugon.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import com.sugon.dao.ToolsDemandPlanItemDao;
import com.sugon.dao.ToolsListDao;
import com.sugon.entity.ToolsDemandPlanItemEntity;
import com.sugon.modules.tools.model.vo.ToolsDemandPlanItemVO;
import com.sugon.service.ToolsDemandPlanItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 备件需求计划明细Service实现类
 *
 * @author sugon
 * @date 2019-08-02 14:31:17
 */
@Service("toolsDemandPlanItemService")
public class ToolsDemandPlanItemServiceImpl implements ToolsDemandPlanItemService {

    @Autowired
    private ToolsDemandPlanItemDao toolsDemandPlanItemDao;
    @Autowired
    private ToolsListDao toolsListDao;

    /**
     *
     * @param id 主键
     * @return
     */
    @Override
    public ToolsDemandPlanItemVO queryObject(String id) {
        return toolsDemandPlanItemDao.queryById(id);
    }

    /**
     *
     * @param planId
     * @param needsPlanId 需求计划ID
     * @return
     */
    @Override
    public boolean isExists(String planId, String needsPlanId) {
        Map<String, Object> param = new HashMap<>(2);
        param.put("planId", planId);
        param.put("materialId", needsPlanId);
        return queryTotal(param) > 0;
    }

    /**
     *
     * @param planId
     * @return
     */
    @Override
    public Double countTotalMoney(String planId) {
        return toolsDemandPlanItemDao.countTotalMoney(planId);
    }

    /**
     *
     * @param map 参数
     * @return
     */
    @Override
    public List<ToolsDemandPlanItemVO> queryList(Map<String, Object> map) {
        return toolsDemandPlanItemDao.listAll(map);
    }

    /**
     *
     * @param needsPlanId 需求计划ID
     * @return
     */
    @Override
    public List<ToolsDemandPlanItemVO> queryByPlanId(String needsPlanId) {
        Map<String, Object> param = new HashMap<>(1);
        param.put("planId", needsPlanId);
        return queryList(param);
    }

    /**
     *
     * @param map 参数
     * @return
     */
    @Override
    public int queryTotal(Map<String, Object> map) {
        return toolsDemandPlanItemDao.queryTotal(map);
    }

    /**
     *
     * @param toolsDemandPlanItem 实体
     * @return
     */
    @Override
    public int save(ToolsDemandPlanItemEntity toolsDemandPlanItem) {
        toolsDemandPlanItem.setId(RandomUtil.randomString(32));
        toolsDemandPlanItem.setGmtCreate(DateUtil.date());
        toolsDemandPlanItem.setGmtModified(toolsDemandPlanItem.getGmtCreate());
        toolsDemandPlanItem.setInboundNum(0);
        toolsDemandPlanItem.setTransitNum(0);
        toolsDemandPlanItem.setIsCancelTransit(0);
        return toolsDemandPlanItemDao.save(toolsDemandPlanItem);
    }

    /**
     *
     * @param toolsDemandPlanItem 实体
     * @return
     */
    @Override
    public int update(ToolsDemandPlanItemEntity toolsDemandPlanItem) {
        toolsDemandPlanItem.setGmtModified(DateUtil.date());
        return toolsDemandPlanItemDao.update(toolsDemandPlanItem);
    }

    /**
     *
     * @param id
     * @return
     */
    @Override
    public int delete(String id) {
        return toolsDemandPlanItemDao.delete(id);
    }

    /**
     *
     * @param ids
     * @return
     */
    @Override
    public int deleteBatch(String[] ids) {
        return toolsDemandPlanItemDao.deleteBatch(ids);
    }

    /**
     *
     * @param toolsNeedsPlanId 工装需求计划ID
     */
    @Override
    public void updateNeedsItemTransitNumByNeedsId(String toolsNeedsPlanId) {
        List<ToolsDemandPlanItemEntity> needsItemList = toolsDemandPlanItemDao.listByPlanId(toolsNeedsPlanId);
        if (CollectionUtil.isNotEmpty(needsItemList)) {
            for (ToolsDemandPlanItemEntity needsItem : needsItemList) {
                needsItem.setTransitNum(needsItem.getDemandNum());
                if (update(needsItem) > 0) {
                    // 实时更新备件在途数
                    toolsListDao.realtimeUpdateTransitNum(needsItem.getMaterialId());
                }
            }
        }
    }
}
